<template>
    <div class="homeIndex"  >
        <header>
            <span @click="showItemize()"><van-icon name="bars"/></span>
            <p @click="searchShow()">
                <van-icon name="search"/>
                <input type="text" placeholder="输入喜欢的宝贝名称"></p>
            <span @click="doLogin()" v-if="bool" style="font-size: 0.24rem">登录</span>
            <span v-else @click="goToMy()"><van-icon name="manager-o"/></span>
        </header>
        <section>
            <Banner></Banner>
            <ShopList></ShopList>
            <ShopListInfo></ShopListInfo>
            <Goods></Goods>
        </section>
        <div class="search" v-show="show">
            <div class="header">
      <span @click="backHome()">
        <van-icon name="cross"/>
      </span>
            <div class="inputSearch">
                <p>
                    <input type="text" v-model="searchInput" placeholder="请输入宝贝名称" autofocus @keyup="searchGoods">
                    <van-icon name="search" @click="searchG()"/>
                </p>
            </div>
            </div>
            <div class="searcHistory">
                <p>历史搜索 <span @click="cleanHistroySearch()"><van-icon name="brush-o" /></span></p>
                <div class="searchBar">
                    <span v-for="(item,index) in searchHistory" @click="SeachItem(item.title)">{{item.title}}</span>
                </div>
            </div>
            <div class="searcHot">
                <p>热门搜索</p>
                <div class="searchBar">
                    <span v-for="(item,index) in hotList" @click="SeachItem(item.title)">{{item.title}}</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    // @ is an alias to /src
    import Banner from "./banner";
    import ShopList from "./shopList";
    import ShopListInfo from "./shopListInfo";
    import Goods from "./goods";
    import {getHotList} from "../../api/list/list";
    export default {
        name: 'home',
        components: {
            Banner, ShopList, ShopListInfo, Goods
        },
        data() {
            return {
                bool: false,
                show: false,
                hotList: [],
                searchInput: '',
                searchHistory:[],
                historyList:[]
            }
        },
        created() {
            if (localStorage.getItem('isLogin')) {
                this.bool = false
            } else {
                this.bool = true
            }
            getHotList().then((res) => {
                console.log(res)
                for(let i=0;i<res.data.data.length;i++){
                    if(res.data.data[i].title == ''){
                        res.data.data.splice(i,1)
                    }
                }
                this.hotList = res.data.data
            })
            if(localStorage.getItem('historySearch')){
                this.searchHistory = JSON.parse(localStorage.getItem('historySearch'))
                this.historyList = JSON.parse(localStorage.getItem('historySearch'))
            }else{
                this.searchHistory = []
            }

        },
        mounted() {
            window.addEventListener('scroll', this.handleScroll, true);  // 监听（绑定）滚轮滚动事件
        },
        methods: {
            handleScroll() {
                let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
                // 设备/屏幕高度
                let scrollTop = document.documentElement.scrollTop;// div 到头部的距离
                let header = document.getElementsByTagName('header')[0]
                if (scrollTop > 100) {
                    header.style.background = "linear-gradient(rgb(235, 22, 37) 0%, rgba(255, 255, 255, 0) 100%)"
                } else {
                    header.style.background = "none"
                }
            },
            doLogin() {
                if (localStorage.getItem('username')) {

                } else {
                    this.$router.push('/login')
                }
            },
            showItemize() {
                this.$router.push('/itemize')
            },
            goToMy() {
                this.$router.push('/my')
            },
            searchShow() {
                this.show = !this.show
            },
            backHome() {
                this.show = !this.show
            },
            SeachItem(titles) {
                this.$router.push({path: '/search', query: {keyWords: titles}})
                const obj = {
                    title:titles
                }
                this.historyList.push(obj)
                localStorage.setItem('historySearch', JSON.stringify(this.historyList))
            },
            searchGoods(e) {
                if (e.keyCode == 13) {
                    this.$router.push({path: '/search', query: {keyWords: this.searchInput}})
                    const obj = {
                        title:this.searchInput
                    }
                    this.historyList.push(obj)
                    localStorage.setItem('historySearch',JSON.stringify(this.historyList))
                }
            },
            searchG() {
                this.$router.push({path: '/search', query: {keyWords:this.searchInput}})
                const obj = {
                    title:this.searchInput
                }
                this.historyList.push(obj)
                localStorage.setItem('historySearch',JSON.stringify(this.historyList))
            },
            cleanHistroySearch(){
                localStorage.removeItem('historySearch')
                this.historyList = []
                this.searchHistory = []
            },
        },
        destroyed() {
            window.removeEventListener('scroll', this.handleScroll, true)
        }

    }
</script>
<style lang="less" scoped>
    .homeIndex {
        width: 100%;
        display: flex;
        flex-direction: column;
        background: #efefef;
        overflow: auto;
        header {
            width: 100%;
            height: 1rem;
            display: flex;
            position: fixed;
            top: 0;
            left: 0;
            z-index: 700;

            span {
                width: 1rem;
                height: 1rem;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 0.4rem;
                color: white;
            }

            p {
                flex: 1;
                display: flex;
                color: white;
                height: 0.6rem;
                margin-top: 0.2rem;
                background: rgba(255, 255, 255, 0.3);
                border-radius: 0.2rem;

                i {
                    font-size: 0.4rem;
                    padding: 0.1rem 0 0 0.1rem;
                    box-sizing: border-box;
                }

                input {
                    background: rgba(255, 255, 255, 0);
                    border: none;
                    padding-left: 0.1rem;
                    box-sizing: border-box;
                }

                input::-webkit-input-placeholder {
                    color: white;
                }
            }
        }

        section {
            flex: 1;

            padding-bottom: 2rem;
            box-sizing: border-box;
        }

        section::-webkit-scrollbar {
            width: 0;
        }

        .search {
            width: 100%;
            height: 100%;
            display: flex;
            position: fixed;
            top: 0;
            left: 0;
            background: white;
            z-index: 800;
            flex-direction: column;

            .header {
                width: 100%;
                height: 1rem;
                display: flex;
                border-bottom: 1px solid #efefef;

                span {
                    width: 1rem;
                    height: 1rem;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 0.4rem;
                }

                .inputSearch {
                    flex: 1;
                    padding: 0.1rem 0.2rem;
                    padding-left: 0;
                    box-sizing: border-box;
                    color: #323232;

                    p {
                        border: 1px solid #B2B2B2;
                        border-radius: 0.1rem;
                        height: 0.6rem;
                        margin-top: 0.1rem;
                        padding-left: 0.1rem;
                        box-sizing: border-box;
                        display: flex;

                        input {
                            flex: 1;
                        }

                        i {
                            font-size: 0.4rem;
                            display: flex;
                            align-items: center;
                            justify-content: center;
                            width: 0.8rem;
                            border-left: 1px solid #B2B2B2;
                            color: #B2B2B2;
                        }
                    }
                }
            }

            .searcHistory {
                width: 100%;

                p {
                    line-height: 0.8rem;
                    padding:0 0.2rem;
                    box-sizing: border-box;
                    display: flex;
                    justify-content: space-between;
                    i{
                        font-size: 0.3rem;
                    }
                }

                .searchBar {
                    width: 100%;
                    padding: 0 0.2rem;
                    box-sizing: border-box;

                    span {
                        width: 30%;
                        float: left;
                        text-align: center;
                        border: 1px solid #efefef;
                        box-sizing: border-box;
                        margin-left: 0.1rem;
                        margin-top: 0.2rem;
                        border-radius: 0.8rem;
                    }
                }
            }
            .searcHot {
                width: 100%;

                p {
                    line-height: 0.8rem;
                    padding-left: 0.2rem;
                    box-sizing: border-box;
                }

                .searchBar {
                    width: 100%;
                    padding: 0 0.2rem;
                    box-sizing: border-box;

                    span {
                        width: 30%;
                        float: left;
                        text-align: center;
                        border: 1px solid #efefef;
                        box-sizing: border-box;
                        margin-left: 0.1rem;
                        margin-top: 0.2rem;
                        border-radius: 0.8rem;
                    }
                }
            }
        }
    }
</style>